<?php

class group extends main {

    function __construct(){
        parent::__construct();
        $this->loadModel('g5_admin_user');
        $this->loadModel('g5_admin_group');
        $this->loadModel('g5_game');
        $this->games = array();

        $this->loadModel('platform');
		//$this->platformList=$this->platform->getPlatList();
		//$this->platformList[0]='mobile';
    }

    function index(){
        $level  = isset($_GET[2]) ? $_GET[2] : 0;
        $group  = $this->g5_admin_group->grouplist($level);
        $this->groups   =  $group;
        $this->level    = $level;
        $this->formAction   = BASEURI.'roup/delete';
        $this->adminlog($this->systemLang['searchUserList']);
    }

    function add(){
        if($group = $_POST['form']){
            $games = empty($_POST['games']) ? array(0) : $_POST['games'];
            $group['group_game'] = implode(',', $games);
            if($insert = $this->g5_admin_group->insert($group)){
                $this->adminlog($this->systemLang['addUserGroupSuccess'].$insert);
                $this->report($this->systemLang['addSuccess'], BASEURI.'group/index');
            }else{
                $this->report($this->systemLang['addFailed'], BASEURI.'group/index');
            }
        }else{
            $this->formAction   = BASEURI.'group/add';
            $this->is_validator = true;
        }
    }

    function edit(){
        $id   = isset($_GET[2]) ? $_GET[2] : 0;
        if($edit  = $_POST['form']){
            $games = empty($_POST['games']) ? array(0) : $_POST['games'];
            //var_dump($games); exit;
            $edit['group_game'] = implode(',', $games);
            if($this->g5_admin_group->update($edit, array('group_id'=>$edit['group_id']))){
                //给某个管理组维护游戏权限的同时自动给下属所有用户增加游戏权限
                $this->g5_admin_user->setGameByGroup($edit['group_id'], $edit['group_game']);
                $this->adminlog($this->systemLang['updateUserGroupSuccess']);
                $this->report($this->systemLang['updateSuccess'], BASEURI.'group/index');
            }else{
                $this->report($this->systemLang['updateFailed'], BASEURI.'group/index');
            }
        }else{
            $group = $this->g5_admin_group->getOne('*', array('group_id'=>$id));
            $this->mygames = explode(',', $group['group_game']);
            $this->formAction   = "/group/edit";
            $this->group        = $group;
            $this->is_validator = "TRUE";
        }
    }

    function delete(){
        $ids   = empty($_POST['select_id']) ? array($_GET[2]) : $_POST['select_id'];
        if($this->g5_admin_group->in(array('group_id'=>$ids))->delete()){
            $this->adminlog($this->systemLang['delUserGroupSuccess'].implode(',', $ids));
            $this->report($this->systemLang['delSuccess'], BASEURI.'group/index');
        }else{
            $this->report($this->systemLang['delFailed'], BASEURI.'group/index');
        }
    }

}

